#!/usr/bin/python
# -*- coding:UTF8 -*-

import pandas as pd
import requests
from lxml import etree

url1 = 'http://www.jkl.com.cn/shopLis.aspx'
User_Agent = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'}

# 1.拿到每个地区的网址
r = requests.get(url=url1, headers=User_Agent).text  # 获取text文本
# print(r)
Analytical_data = etree.HTML(r)  # 转html格式
# print(Analytical_data)
Each_City = Analytical_data.xpath('//div[@class="infoLis"]//@href')  # 定位每个城区的位置
# print(Each_City)

# 2.对每个城区进行解析
for city in Each_City:
    # print(city)
    url2 = 'https://www.jkl.com.cn/' + city  # 拼接每个城区的网址
    # print(url2)
    r = requests.get(url=url2, headers=User_Agent).text
    # print(r)
    Analytical_data = etree.HTML(r)
    Store_Name = Analytical_data.xpath('//span[@class="con01"]/text()')  # 对店名定位
    Business_Address = Analytical_data.xpath('//span[@class="con02"]/text()')  # 对经营地址定位
    Telephone = Analytical_data.xpath('//span[@class="con03"]/text()')  # 对电话定位
    Business_Hours = Analytical_data.xpath('//span[@class="con04"]/text()')  # 对营业时间定位
    # print(Store_Name)
    # print(Business_Address)

    # 数据放入表格
    table = pd.DataFrame({
        "店铺名称": Store_Name,
        "经营地址": Business_Address,
        "电话号码": Telephone,
        "经营时间": Business_Hours
    })
    table.to_csv('./测试数据/店铺信息.csv', encoding='ANSI', index=0, mode='a', header=0)
    if url2 == 'https://www.jkl.com.cn/shopLis.aspx?TypeId=10045':
        for current in range(2, 4):
            params = {
                "current": current,
                "TypeId": 10045
            }
            data = requests.get(url='https://www.jkl.com.cn/shopLis.aspx', params=params, headers=User_Agent).text
            # print(data)
            Analytical_data = etree.HTML(data)
            # print(Analytical_data)
            Store_Name = Analytical_data.xpath('//span[@class="con01"]/text()')  # 对店名定位
            Business_Address = Analytical_data.xpath('//span[@class="con02"]/text()')  # 对经营地址定位
            Telephone = Analytical_data.xpath('//span[@class="con03"]/text()')  # 对电话定位
            Business_Hours = Analytical_data.xpath('//span[@class="con04"]/text()')  # 对营业时间定位
            # print(Business_Hours)
            table = pd.DataFrame({
                "店铺名称": Store_Name,
                "经营地址": Business_Address,
                "电话号码": Telephone,
                "经营时间": Business_Hours
            })
            table.to_csv('./测试数据/店铺信息.csv', encoding='ANSI', index=0, mode='a', header=0)
